<?php

namespace app\api\controller;

use app\api\model\UsersGroup;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use app\admin\model\usermanage\Users as UsersModel;

/**
 * 会员接口
 */
class Users extends Api
{
    protected $noNeedLogin = ['login', 'forgetpwd','resetpwd'];
    protected $noNeedRight = '*';

    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @ApiParams (name="account", type="string", required=true, description="账号")
     * @ApiParams (name="password", type="string", required=true, description="密码")
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        $code = input('code','');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password,$code);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    //忘记密码
    public function forgetpwd()
    {
        $mobile = $this->request->post("mobile");
        $newpassword = $this->request->post("newpassword");
        $captcha = $this->request->post("captcha");

        if (!$newpassword || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        //验证Token
        if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
            $this->error(__('Password must be 6 to 30 characters'));
        }

        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('手机号格式错误');
        }
        $user = UsersModel::get(['binding'=>$mobile]);
        if (!$user) {
            $this->error('用户不存在');
        }

        $ret = Sms::check($mobile, $captcha, 'resetpwd');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        Sms::flush($mobile, 'resetpwd');

        $ret = $this->auth->changepwd($newpassword, '', true);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 重置密码
     *
     * @ApiMethod (POST)
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="newpassword", type="string", required=true, description="新密码")
     * @ApiParams (name="captcha", type="string", required=true, description="验证码")
     */
    public function resetpwd()
    {
        $password = $this->request->post("password");
        $newpassword = $this->request->post("newpassword");

        if (!$newpassword) {
            $this->error(__('Invalid parameters'));
        }
        //验证Token
        if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
            $this->error(__('Password must be 6 to 30 characters'));
        }

        $ret = $this->auth->changepwd($newpassword, $password);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }

    //修改昵称/头像
    public function profile()
    {
        $user = $this->auth->getUser();

        $nickname = $this->request->post('nickname');
        $avatar_url = $this->request->post('avatar_url', '', 'trim,strip_tags,htmlspecialchars');

        if ($avatar_url){
            $user->avatar_url = $avatar_url;
        }
        if ($nickname){
            $user->nickname = $nickname;
        }
        $user->save();
        $this->success();

    }

    //用户信息
    public function index()
    {
        $data = ['userinfo' => $this->auth->getUserinfo()];
        $this->success('', $data);
    }

    //注册
    public function register()
    {
        $account = input('account','');
        if (empty($account)){
            $this->error('请输入账号');
        }
        $password = input('password','123456');
        if (empty($password)){
            $this->error('请输入密码');
        }
        $realname = input('realname','');
        if (empty($realname)){
            $this->error('请输入姓名');
        }

        $binding = input('binding','');
        if (empty($binding)){
            $this->error('请输入手机号');
        }

        $id_card_number = input('id_card_number','');
        if (empty($id_card_number)){
            $this->error('请输入身份证号');
        }
        $parent = input('parent','');
        if (empty($parent)){
            $this->error('请选择推荐人');
        }
        $contact = input('contact','');
        if (empty($parent)){
            $this->error('请选择接点人');
        }
        $group_id = input('group_id',0);
        if (empty($group_id)){
            $this->error('请选择所在组');
        }
        $ret = $this->auth->register($account,$password,$realname,$binding,$id_card_number,$parent,$contact,$group_id);

        if ($ret) {
            $this->success('注册成功');
        } else {
            $this->error($this->auth->getError());
        }
    }

    //根据账号和姓名查用户
    public function searchUser()
    {
        $account = input('account','');
        if (empty($account)){
            $this->error('请输入账号');
        }

        $realname = input('realname','');
        if (empty($realname)){
            $this->error('请输入姓名');
        }

        $user = UsersModel::get(['realname'=>$realname,'account'=>$account]);
        if (!$user){
            $this->error('用户不存在');
        }

        $this->success('',$user);
    }

    //分组列表
    public function group()
    {
        $type = input('type',0);
        $account = input('contact_account','');
        if (empty($account)){
            $this->error('请输入接点人账号');
        }
        $contact_info = UsersModel::get(['account'=>$account]);
        if (!$contact_info){
            $this->error('接点人不存在');
        }

        $group = UsersGroup::where(['type'=>$type,'user_id'=>$contact_info->id])->select();

        $this->success('',$group);
    }

    //用户编号生成
    public function getAccount()
    {
        $account = null;
        while (true) {
            $account = 'z'.rand(1000000,9999999);
            $exist_order_no = UsersModel::where(['account' => $account])->find();
            if (!$exist_order_no) {
                break;
            }
        }
        $this->success('',$account);
    }

    //设置支付密码
    public function setPayPwd()
    {
        $mobile = $this->request->post("mobile");
        $pay_pwd = $this->request->post("pay_pwd");
        $captcha = $this->request->post("captcha");

        if (!$pay_pwd || !$captcha) {
            $this->error(__('Invalid parameters'));
        }

        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('手机号格式错误');
        }
        $user = UsersModel::get(['binding'=>$mobile]);
        if (!$user) {
            $this->error('用户不存在');
        }

        $ret = Sms::check($mobile, $captcha, 'set_pay_pwd');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        Sms::flush($mobile, 'set_pay_pwd');

        $ret = $this->auth->setpaypwd($this->auth->id,$pay_pwd);
        if ($ret) {
            $this->success('支付密码设置成功');
        } else {
            $this->error($this->auth->getError());
        }
    }

    //修改支付密码
    public function editPayPwd()
    {
        $mobile = $this->request->post("mobile");
        $pay_pwd = $this->request->post("pay_pwd");
        $new_pay_pwd = $this->request->post("new_pay_pwd");
        $captcha = $this->request->post("captcha");

        if (!$pay_pwd || !$new_pay_pwd || !$captcha) {
            $this->error(__('Invalid parameters'));
        }

        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('手机号格式错误');
        }
        $user = UsersModel::get(['binding'=>$mobile]);
        if (!$user) {
            $this->error('用户不存在');
        }

        $ret = Sms::check($mobile, $captcha, 'pay_pwd');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        Sms::flush($mobile, 'pay_pwd');

        $ret = $this->auth->editpaypwd($this->auth->id,$pay_pwd,$new_pay_pwd);
        if ($ret) {
            $this->success('支付密码修改成功');
        } else {
            $this->error($this->auth->getError());
        }
    }


}